//
//  XFHomeTableViewController.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/10.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFHomeTableViewController.h"
#import "XFUserAccountModel.h"
#import "XFStatues.h"
#import "XFStatueCell.h"
#import "XFStatuesViewModel.h"
#import "SVProgressHUD.h"
typedef void (^FinishedHome)(NSError *error, NSArray *array);
static NSString *cellId = @"statues";
@interface XFHomeTableViewController ()

@property(nonatomic, strong) NSMutableArray *statues;

@property(nonatomic, strong) UIActivityIndicatorView *indicatorView;

@end

@implementation XFHomeTableViewController

//- (UIActivityIndicatorView *)indicatorView{
//
//    if (_indicatorView) {
//        _indicatorView = [[UIActivityIndicatorView alloc]
//                          initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    }
//    return _indicatorView;
//}

- (NSMutableArray *)statues {

  if (!_statues) {
    _statues = [NSMutableArray array];
  }
  return _statues;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  if (!self.isLogin) {
    [self.visitView
        setupSubViewsWithImage:
            nil andMessage:@"关注一些人，回这里看看有什么惊喜"];
    return;
  }
  [self prepareCellAndloadData];
}

- (void)prepareCellAndloadData {

  //注册 cell
  [self.tableView registerClass:[XFStatueCell class]
         forCellReuseIdentifier:cellId];

  [self loadData];

  self.refreshControl = [[UIRefreshControl alloc] init];

  [self.refreshControl addTarget:self
                          action:@selector(loadData)
                forControlEvents:UIControlEventValueChanged];

  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  //    self.tableView.rowHeight = 200;
  self.tableView.estimatedRowHeight = 200;
  self.tableView.rowHeight = UITableViewAutomaticDimension;

  UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]
      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  self.indicatorView = indicatorView;
  self.tableView.tableFooterView = self.indicatorView;
}

- (void)loadData {

  XFStatuesViewModel *statuesViewModel = [[XFStatuesViewModel alloc] init];

  NSInteger since_id = 0;
  NSInteger max_id = 0;

  if ([self.indicatorView isAnimating]) {
    since_id = 0;
    XFStatues *sttuess = self.statues.lastObject;
    max_id = sttuess.ids;
    NSLog(@"max_id---%ld", max_id);
  } else {
    XFStatues *sttuess = self.statues.firstObject;
    since_id = sttuess.ids;
    max_id = 0;
    NSLog(@"since_id---%ld", since_id);
  }
  // since_id = [self.statues.firstObject ids];

  [statuesViewModel
      loadDataAndSinceId:since_id
                   MaxId:max_id
                Finished:^(NSError *error, NSArray *array) {

                  [self.refreshControl endRefreshing];
                    
                  NSLog(@"%lu", (unsigned long)array.count);
                    
                  if (array == nil) {
                    [self.indicatorView stopAnimating];
                    [SVProgressHUD showErrorWithStatus:@"网络错误"];
                    NSLog(@"%@", error);
                    return;
                  }

                  if (since_id > 0) {
                    //下拉刷新数据
                    NSIndexSet *set = [NSIndexSet
                        indexSetWithIndexesInRange:NSMakeRange(0, array.count)];
                    [self.statues insertObjects:array atIndexes:set];

                    //                    self.statues =
                    //                        [arrs
                    //                        arrayByAddingObjectsFromArray:self.statues];

                  } else if (max_id > 0) {
                    //上啦刷新数据
                    //初始一个添加数组的范围
                      [self.indicatorView stopAnimating];
//                    [self.statues arrayByAddingObjectsFromArray:array];
                      [self.statues addObjectsFromArray:array ];
                  } else {
                    //第一次加载数据
                    self.statues = [NSMutableArray arrayWithArray:array];
                  }

                  [self.tableView reloadData];

                }];

  //  [statuesViewModel loadDataAndFinished:^(NSError *error, NSArray *array) {
  //    if (error != nil) {
  //      [SVProgressHUD showErrorWithStatus:@"网络错误"];
  //      NSLog(@"%@", error);
  //      return;
  //    }
  //    self.statues = array;
  //    [self.tableView reloadData];
  //  }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {

  return self.statues.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  XFStatueCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId
                                                       forIndexPath:indexPath];

  XFStatues *statue = self.statues[indexPath.row];
  cell.statues = statue;

  if (indexPath.row == self.statues.count - 2 &&
      ![self.indicatorView isAnimating]) {
    [self.indicatorView startAnimating];
    NSLog(@"加载更多数据");
    [self loadData];
  }

  return cell;
}

@end
